package com.cm.springcloud.service.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.cm.springcloud.service.service.Service2Feign;
import com.cm.springcloud.service.vo.SimpleVo;
import com.cm.springcloud.service.vo.SimpleVo2;
@RestController
@RequestMapping("service1")
public class ServiceController {
	
	@Value("${server.port}")
    private String serverPort;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Service2Feign service2Feign;
    @RequestMapping("req1")
    public String f1() {
    	return service2Feign.f1("hello feign");
    }
    @RequestMapping("req2")
    public SimpleVo f2() {
    	SimpleVo vo = new SimpleVo();
    	vo.setP1("p1");
    	vo.setP2("p2");
    	SimpleVo2 vo2 = new SimpleVo2();
    	vo2.setP1("vo2.p1");
    	vo2.setP2("vo2.p2");
    	vo.setVo2(vo2);
    	return service2Feign.f2(vo);
    }
    
	@RequestMapping("http1")
	public String service() {
		String url = "http://service2/service2/http1";
		String result = restTemplate.getForObject(url, String.class);
		return result;
	}
	@RequestMapping("http2")
	public String service2() {
		String url = "http://service2/service2/http1";
		String result = restTemplate.getForObject(url, String.class);
		return result;
	}
	@Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
